﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Classes;

namespace OfficeManager_v0._1
{
    public partial class AddOrEditProject : Form
    {
        Organization mOrganization;
        bool edit;
        Project oldProject;

        public AddOrEditProject(Organization aOrganization, bool edit, Project project = null)
        {
            mOrganization = aOrganization;
            this.edit = edit;
            InitializeComponent();
            PopulateDepartmentMenu();

            comboBoxDepartment.DropDownStyle = ComboBoxStyle.DropDownList;
            if (edit)
            {
                oldProject = project;
                textBoxName.Text = project.Subject;
                textBoxBudget.Text = project.Budget.ToString();
                textBoxNote.Text = project.Note;

                if (oldProject.Department != null)
                    comboBoxDepartment.SelectedItem = oldProject.Department.Name + " ID:" + oldProject.Department.ID;
            }
        }

        private void PopulateDepartmentMenu()
        {
            comboBoxDepartment.Enabled = (mOrganization.Departments.Count != 0);
            foreach (Department department in mOrganization.Departments)
                comboBoxDepartment.Items.Add(department.Name + " ID:" + department.ID);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                Department target = null;
                if (comboBoxDepartment.SelectedItem != null)
                    target = mOrganization.Departments.Find((dep) =>
                        {
                            return dep.Name + " ID:" + dep.ID == comboBoxDepartment.GetItemText(comboBoxDepartment.SelectedItem);
                        });
                if (comboBoxDepartment.GetItemText(comboBoxDepartment.SelectedItem) != "" && target == null)
                    throw new Exception();
                
                if (!edit)
                {
                    Project project = new Project();
                    project.Subject = textBoxName.Text;
                    project.Budget = int.Parse(textBoxBudget.Text);
                    project.Note = textBoxNote.Text;
                    project.Department = target;
                    if (target != null)
                        target.Projects.Add(project);

                    mOrganization.Projects.Add(project);
                }
                else
                {
                    oldProject.Subject = textBoxName.Text;
                    oldProject.Budget = int.Parse(textBoxBudget.Text);
                    oldProject.Note = textBoxNote.Text;

                    if (oldProject.Department != null)
                        oldProject.Department.Projects.Remove(oldProject);
                    oldProject.Department = target;
                    if (target != null)
                        target.Projects.Add(oldProject);
                }

            }
            catch (Exception exc)
            {
                MessageBox.Show("Въведените данни са некоректни!");
                return;
            }

            this.Close();
            this.Dispose();
        }
    }
}
